package main

import (
	"flag"
	"fmt"
	"go-scrot/util"
	"image"
	"time"
)

func main() {
	output := flag.String("o", "", "输出文件路径")
	delay := flag.Int("d", 0, "延迟截图时间")
	help := flag.Bool("h", false, "显示帮助")
	format := flag.String("f", "png", "图像格式[png,jpg,jpeg,bmp,webp,tiff]")
	selection := flag.Bool("s", false, "选择截图区域")
	flag.Parse()

	if *help {
		util.ShowHelp()
		return
	}

	if *delay > 0 {
		// 实时更新剩余秒数
		fmt.Printf("延迟 %d 秒后截图...\n", *delay)
		for i := *delay; i > 0; i-- {
			// 使用\r回到行首，覆盖当前行内容
			fmt.Printf("\r剩余 %d 秒...", i)
			time.Sleep(1 * time.Second)
		}
		// 清空倒计时行，避免残留
		fmt.Printf("\r                      \r")
	}

	if *output == "" {
		*output = util.GenerateDefaultFileName(*format)
	}

	if err := util.EnsureDir(*output); err != nil {
		fmt.Printf("创建目录失败: %v\n", err)
		return
	}

	var err error
	var img image.Image
	if *selection {
		img, err = util.CaptureScreenSelection()
	} else {
		img, err = util.CaptureScreen()
	}
	if err == nil && img != nil {
		err = util.SaveImage(img, *output, *format)
	}
	if err != nil {
		fmt.Printf("截图并保存失败: %v\n", err)
		return
	}
	fmt.Printf("截图已保存到: %s\n", *output)
}
